﻿using System;
using System.Collections;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.TimeIterate
{
  /// <summary>
  /// 季度时间迭代器的Handler
  /// </summary>
  public class QuarterIterateHandler : TimeIterateBaseHandler
  {
    protected override IList CreateTimeList(DateTime startDate, DateTime endDate)
    {
      var listTimes = new ArrayList();
      var endQuarterId = endDate.QuarterId();
      var quarterNow = DateTime.Now.QuarterId();
      for (var date = startDate; date.QuarterId() <= endQuarterId; date = date.NextQuarter())
      {
        listTimes.Add(new Hashtable()
        {
          {
            TIME_ID_FIELD, date.QuarterId ( )
          },
          {
            YEAR_ID_FIELD, date.Year
          },
          {
            START_DATE_FIELD, date.QuarterFirstDay ( ).Value.NotLessThan ( startDate ).ToString( "yyyy-MM-dd" )
          },
          {
            END_DATE_FIELD, date.QuarterLastDay ( ).Value.NotGreaterThan ( endDate ).ToString( "yyyy-MM-dd" )
          },
          {
            IS_CURRENT_TIME_FIELD, date.QuarterId() == quarterNow
          }
        });
      }
      return listTimes;
    }
  }
}
